global root_dir = "`1'"
include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/tab_A41_data.log, replace name(dat)

capture noi {
	
    
    * ----------------------------------------------------- *
	* Prep Auto95_bia vars
	* ----------------------------------------------------- *

	* get all auto95 patent applications of auto95 baseline reg firms
	use ${dataset_dir}/patstat_orbis/Orbis_patents_list_2017_merged.dta, clear
	keep if ipr_type==2
	mmerge appln_id using ${commondata_dir}/patstat_2018b/appln_info.dta, unmatched(master) ukeep(granted)
	mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95.dta, unmatched(master)
	keep BvDIDnumber appln_id granted
	mmerge appln_id using ${dataset_dir}/patent_list/auto95_patents.dta, unmatched(none)
	gen auto95=(_m==3)

	* go to family level and merge citation data + biadic. Only keep biadic
	mmerge appln_id using ${commondata_dir}/patstat_2018b/family_info.dta, unmatched(master) ukeep(docdb_family_id fam_earliest_appln_year)
	ren fam_earliest_appln_year year
	drop if year == 9999
	mmerge docdb_family_id using ${dataset_dir}/patstat_orbis/citations_by_docdb_id_exclself_normalized.dta, unmatched(master) ukeep(cit_5yrs_norm) 
	bys docdb_family_id : egen fam_auto95 = max(auto95)
	mmerge docdb_family_id using ${dataset_dir}/patstat_orbis/docdb_families2.dta, unmatched(master) ukeep(biadic_D)
	ren biadic_D bia
	keep if fam_auto95 == 1 & bia == 1
	keep BvD year docdb_family_id cit_5yrs_norm fam_auto95 bia
	duplicates drop 

	* ----------------------------------------------------- *
	* Create citation-weighted bounded dep vars
	* ----------------------------------------------------- *

	* Include self-citation (for the "what we currently do")
	replace cit_5yrs_norm = 0 if missing(cit_5yrs_norm)
	replace cit_5yrs_norm = 1 + cit_5yrs_norm

	* create bounded versions 
	foreach citvar in cit_5yrs_norm { 
		local percentiles 75 90 95 99 
		foreach pt of local percentiles { 
			egen p`pt'= pctile(`citvar'), p(`pt')
			gen `citvar'_bp`pt' = `citvar'
			replace `citvar'_bp`pt' = p`pt' if `citvar' >= p`pt'
			drop p`pt'
		}
		egen p999 = pctile(`citvar'), p(99.9)
		gen `citvar'_bp999 = `citvar'
		replace `citvar'_bp999 = p999 if `citvar' >= p999
		drop p999
		* as a cross-check / comparison
		gen `citvar'_bp100 = `citvar' 
	}

	log using ${numb_dir}/Table_A41_citations_numbers.log, replace name(numb)
	su cit_5yrs_norm cit_5yrs_norm_bp*
	log close numb

	* ----------------------------------------------------- *
	* Sum up by BvD year save
	* ----------------------------------------------------- *

	foreach xxx in auto95 {
		foreach ttt in bia {
			bys BvD year : egen cbp75`xxx'_`ttt' = sum(fam_`xxx'*`ttt'*cit_5yrs_norm_bp75)
			bys BvD year : egen cbp90`xxx'_`ttt' = sum(fam_`xxx'*`ttt'*cit_5yrs_norm_bp90)
			bys BvD year : egen cbp95`xxx'_`ttt' = sum(fam_`xxx'*`ttt'*cit_5yrs_norm_bp95)
			bys BvD year : egen cbp99`xxx'_`ttt' = sum(fam_`xxx'*`ttt'*cit_5yrs_norm_bp99)
			bys BvD year : egen cbp999`xxx'_`ttt' = sum(fam_`xxx'*`ttt'*cit_5yrs_norm_bp999)
			bys BvD year : egen cbp100`xxx'_`ttt' = sum(fam_`xxx'*`ttt'*cit_5yrs_norm_bp100)
		}
	}
	keep BvD year cbp* 
	duplicates drop
	compress
	save ${final_dir}/auto95_citbound.dta, replace

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat